﻿using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ContactSMS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class addMessageAndMessageSessionTable : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<long>(
                name: "ContactId",
                table: "T_PhoneRecord",
                type: "bigint",
                nullable: false,
                comment: "联系人id",
                oldClrType: typeof(long),
                oldType: "bigint");

            migrationBuilder.CreateTable(
                name: "T_Message",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    MessageSessionId = table.Column<long>(type: "bigint", nullable: false, defaultValue: 0L, comment: "短信会话ID"),
                    Desc = table.Column<string>(type: "longtext", nullable: false, comment: "消息内容")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    IsRead = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "是否已读"),
                    ContactId = table.Column<long>(type: "bigint", nullable: false, comment: "联系人id"),
                    CreatTime = table.Column<long>(type: "bigint", nullable: false, comment: "创建时间"),
                    UpdateTime = table.Column<long>(type: "bigint", nullable: false, comment: "更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Message", x => x.Id);
                },
                comment: "短信表")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "T_MessageSession",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Mobile = table.Column<string>(type: "VARCHAR(20)", nullable: false, comment: "电话")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Desc = table.Column<string>(type: "longtext", nullable: true, comment: "消息内容")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    ContactId = table.Column<long>(type: "bigint", nullable: false, comment: "联系人id"),
                    CreatTime = table.Column<long>(type: "bigint", nullable: false, comment: "创建时间"),
                    UpdateTime = table.Column<long>(type: "bigint", nullable: false, comment: "更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_MessageSession", x => x.Id);
                },
                comment: "短信会话表")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_T_PhoneRecord_ContactId",
                table: "T_PhoneRecord",
                column: "ContactId");

            migrationBuilder.AddForeignKey(
                name: "FK_T_PhoneRecord_T_Contact_ContactId",
                table: "T_PhoneRecord",
                column: "ContactId",
                principalTable: "T_Contact",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_T_PhoneRecord_T_Contact_ContactId",
                table: "T_PhoneRecord");

            migrationBuilder.DropTable(
                name: "T_Message");

            migrationBuilder.DropTable(
                name: "T_MessageSession");

            migrationBuilder.DropIndex(
                name: "IX_T_PhoneRecord_ContactId",
                table: "T_PhoneRecord");

            migrationBuilder.AlterColumn<long>(
                name: "ContactId",
                table: "T_PhoneRecord",
                type: "bigint",
                nullable: false,
                oldClrType: typeof(long),
                oldType: "bigint",
                oldComment: "联系人id");
        }
    }
}
